package com.shizhanzhe.gxb.vip.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import me.wcy.htmltext.HtmlImageLoader;
import me.wcy.htmltext.HtmlText;

/**
 * Created by YJ on 2019/1/21.
 */
public class NoticeDetailActivity extends SwipeBackActivity {
    @BindView(R.id.web)
    WebView webView;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.top_title)
    TextView topTitle;
    private String details = "";
    private String contents = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticedetail);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Log.d("____",getIntent().getIntExtra("id",0)+"id");
        topTitle.setText(getIntent().getStringExtra("title"));
        details = getIntent().getStringExtra("details");
        contents = getIntent().getStringExtra("content");
        webView.setBackgroundColor(0);
        webView.getSettings().setJavaScriptEnabled(false);
        webView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setUseWideViewPort(true);//设置webview显示屏幕宽度 不能滑动
        webView.getSettings().setLoadWithOverviewMode(true);
        content.setMovementMethod(ScrollingMovementMethod.getInstance());// 设置可滚动
        int type = getIntent().getIntExtra("type", 0);
        setDetails();
    }

    private void setDetails(){
        if (!"".equals(details)) {
            String json = details.replace("src=\"", "src=\""+ Path.BASE_URL);
            webView.loadDataWithBaseURL(null, json, "text/html", "utf-8", null);
//                content.setText(Html.fromHtml(json,new MyImageGetter(getApplicationContext(),content),null));
            HtmlText.from(json)
                    .setImageLoader(new HtmlImageLoader() {
                        @Override
                        public void loadImage(String url, final Callback callback) {
                            // Glide sample, you can also use other image loader
                            Glide.with(getApplicationContext()).asBitmap()
                                    .load(url)
                                    .into(new SimpleTarget<Bitmap>() {
                                        @Override
                                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                            callback.onLoadComplete(resource);
                                        }
                                    });
                        }

                        @Override
                        public Drawable getDefaultDrawable() {
                            return ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher);
                        }

                        @Override
                        public Drawable getErrorDrawable() {
                            return ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher);
                        }

                        @Override
                        public int getMaxWidth() {
                            return Util.getScreenWidth(getApplicationContext());
                        }

                        @Override
                        public boolean fitWidth() {
                            return false;
                        }
                    }).into(content);
        } else {
            content.setText(contents);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }
}
